Skip to content

Conversation

@bsbodden
Copy link
Collaborator

When a document has a @reference @indexed field pointing to another entity, the metamodel now generates field accessors for the referenced entity's @indexed and @searchable fields. This enables queries like:

entityStream.of(RefVehicle.class)
.filter(RefVehicle$.OWNER_NAME.eq("John"))

Changes:

  • MetamodelGenerator: Add processReferencedEntityIndexableFields() to traverse referenced entities and generate subfield accessors
  • RediSearchIndexer: Add createIndexedFieldsForReferencedEntity() to create search index fields for referenced entity properties

For example, given:
@document class Owner { @searchable String name; @indexed String email; }

@document class RefVehicle { @reference @indexed Owner owner; }

The RefVehicle$ metamodel now includes:

  • OWNER_NAME (TextField)
  • OWNER_EMAIL (TextTagField)

Note: @reference fields store only the entity ID, not the full embedded object. To actually search by referenced entity properties, the data must be denormalized or the query should use ReferenceField.eq(entity).

Closes #677

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds support for generating indexed subfields for @Reference fields in the metamodel, enabling queries on referenced entity properties. When a document has a @Reference @Indexed field pointing to another entity, the metamodel now generates field accessors for the referenced entity's @Indexed and @Searchable fields.

Key Changes

  • MetamodelGenerator: Added processReferencedEntityIndexableFields() to traverse referenced entities and generate subfield accessors for indexed/searchable fields
  • RediSearchIndexer: Added createIndexedFieldsForReferencedEntity() to create search index fields for referenced entity properties
  • Test fixtures: Added Owner, RefVehicle models and repositories, along with comprehensive tests to verify metamodel generation and query behavior

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 11 comments.

Show a summary per file
File Description
redis-om-spring/src/main/java/com/redis/om/spring/metamodel/MetamodelGenerator.java Added processReferencedEntityIndexableFields() method to generate metamodel fields for referenced entity's indexed fields
redis-om-spring/src/main/java/com/redis/om/spring/indexing/RediSearchIndexer.java Added createIndexedFieldsForReferencedEntity() method to create index fields for referenced entity properties
tests/src/test/java/com/redis/om/spring/fixtures/document/model/Owner.java New test fixture representing an entity with indexed fields that can be referenced
tests/src/test/java/com/redis/om/spring/fixtures/document/model/RefVehicle.java New test fixture with a @Reference @Indexed field to Owner, demonstrating the feature
tests/src/test/java/com/redis/om/spring/fixtures/document/repository/OwnerRepository.java Repository for Owner entity
tests/src/test/java/com/redis/om/spring/fixtures/document/repository/RefVehicleRepository.java Repository for RefVehicle entity
tests/src/test/java/com/redis/om/spring/annotations/document/ReferenceIndexedSubfieldsTest.java Comprehensive tests verifying metamodel field generation and query behavior for referenced entity fields

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 5 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

…dis#677)

When a document has a @reference @indexed field pointing to another entity,
the metamodel now generates field accessors for the referenced entity's
@indexed and @searchable fields. This enables queries like:

  entityStream.of(RefVehicle.class)
    .filter(RefVehicle$.OWNER_NAME.eq("John"))

Changes:
- MetamodelGenerator: Add processReferencedEntityIndexableFields() to
  traverse referenced entities and generate subfield accessors
- RediSearchIndexer: Add createIndexedFieldsForReferencedEntity() to
  create search index fields for referenced entity properties

Supported annotation attributes for referenced entity fields:
- @searchable: weight, sortable, nostem, noindex, phonetic, indexMissing, indexEmpty
- @TextIndexed: weight, sortable, nostem, noindex, phonetic, indexMissing, indexEmpty
- @indexed: sortable, separator, indexMissing, indexEmpty
- @TagIndexed: separator, indexMissing, indexEmpty
- @NumericIndexed: sortable, noindex, indexMissing
- @indexed (Boolean): sortable, indexMissing, indexEmpty
- @indexed (Enum): sortable, separator, indexMissing, indexEmpty

For example, given:
  @document class Owner {
    @searchable String name;
    @indexed String email;
    @TagIndexed String category;
    @NumericIndexed Integer age;
    @indexed Boolean active;
  }

  @document class RefVehicle {
    @reference @indexed Owner owner;
  }

The RefVehicle$ metamodel now includes:
  - OWNER_NAME (TextField)
  - OWNER_EMAIL (TextTagField)
  - OWNER_CATEGORY (TextTagField)
  - OWNER_AGE (NumericField)
  - OWNER_ACTIVE (TextTagField)

Note: @reference fields store only the entity ID, not the full embedded
object. To actually search by referenced entity properties, the data
must be denormalized or the query should use ReferenceField.eq(entity).

Closes redis#677
@bsbodden bsbodden merged commit c79455d into redis:main Nov 25, 2025
4 checks passed
@bsbodden bsbodden deleted the bsb/issue-677 branch November 26, 2025 17:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fails to create Indexed subfields when indexing reference fields

1 participant